const app = getApp();
const util = require('../../utils/util.js');
Page({
  data: {
    priceMeal: [],
    leaseTime: 0,
    sn: "",
    isClick: true,
    amount: 0,
    order_id: "",
    checkNum: 0,
    timer: '',
    storeName: "",
    is_show: 0,
    need_tel: 0
  },
  onLoad(options) {
    my.showLoading({
      content: 'loading...'
    });
    let json = JSON.parse(options.data);
    this.setData({
      info: json,
      priceMeal: json.priceMeal,
      leaseTime: json.leaseTime,
      sn: json.sn,
      amount: json.leaseMoney,
      storeName: json.storeName,
      is_show: json.is_show,
      need_tel: json.need_tel
      // need_tel: 1
    });
    my.hideLoading();
  },
  onUnload() {
    clearInterval(this.data.timer);
  },
  onHide() {
    clearInterval(this.data.timer);
  },
  // ==================== 点击单选框 ====================================
  radioChange(e) {
    let items = this.data.priceMeal;
    let time = e.detail.value;
    let amount = 0;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].time != time) {
        items[i].checked = false;
      } else {
        items[i].checked = true;
        amount = items[i].amount;
      }
    }
    this.setData({
      priceMeal: items,
      leaseTime: time,
      amount: amount
    });
  },
  // ==================== 选择套餐时间 ====================================
  choseTime(e) {
    let that = this;
    let time = e.currentTarget.dataset.time;
    let items = this.data.priceMeal;
    let amount = 0;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].time != time) {
        items[i].checked = false;
      } else {
        items[i].checked = true;
        amount = items[i].amount;
      }
    }
    this.setData({
      priceMeal: items,
      leaseTime: time,
      amount: amount
    });
  },
  // ============================ 租借充电线 =============================
  LineBorrow() {
    let that = this;
    if (that.data.isClick) {
      let order_id = that.data.order_id;
      let amount = that.data.amount;
      my.showLoading({
        content: '请稍后...'
      });
      that.setData({
        isClick: false
      });
      if (that.data.leaseTime > 0) {
        util.httpRequest('/LineBorrow', {
          sn: that.data.sn,
          time: that.data.leaseTime,
          amount: amount,
          order_id: order_id
        }, function (res) {
          switch (res.code) {
            case 1:
              //租借
              // that.setData({
              //     isClick: true
              // });
              my.hideLoading();
              // 跳转到线密码界面
              my.navigateTo({
                url: '../linePassword/linePassword?data=' + JSON.stringify(res.data)
              });
              break;
            case 3:
              // 未支付，发起支付
              my.hideLoading();
              my.showLoading({
                content: '请稍后...'
              });
              util.alipayPayment(res.data.amount, 3, res.data.sn, res.data.time, function (order_id) {
                my.hideLoading();
                if (order_id != null) {
                  that.checkOrderStatus(order_id);
                } else {
                  that.setData({
                    isClick: true
                  });
                }
              }, function () {
                that.setData({
                  isClick: true
                });
                my.hideLoading();
              });
              break;
            default:
              that.setData({
                isClick: true
              });
              my.hideLoading();
              break;
          }
        }, function () {
          that.setData({
            isClick: true
          });
          my.hideLoading();
        })
      } else {
        that.setData({
          isClick: true
        });
        my.showToast({
          content: '请选择对应的价格套餐',
          type: 'none'
        });
      }
    }
  },
  // ====================== 校验用户是否支付成功  ===========================================
  checkOrderStatus(order_id) {
    let that = this;
    let check = that.data.checkNum;
    check++;
    that.setData({
      checkNum: check
    });
    if (that.data.checkNum < 20) {
      util.httpRequest('/checkOrderStatus', {
        order_id: order_id,
        freeDeposit: 0
      }, function (res) {
        if (res.data.status == 1) {
          my.hideLoading();
          that.setData({
            order_id: res.data.order_id,
            isClick: true
          });
          clearInterval(that.data.timer);
          that.LineBorrow();
        } else {
          var timerTem = setTimeout(() => {
            that.checkOrderStatus(order_id);
          }, 1500);
          that.setData({
            timer: timerTem
          });
        }
      }, function () {
        my.hideLoading();
      })
    } else {
      my.showToast({
        content: '订单异常,请联系客服',
        duration: 2000
      });
      my.hideLoading();
    }
  },

  // ========================== 手机号授权start ============================

  // 用户取消授权
  onAuthError() {
    let that = this;
    // 用户拒绝
    let refuseShow = that.data.info.refuse_show;
    let refuseAfterToBorrow = that.data.info.refuse_after_borrow;
    if (refuseShow == 1) {
      my.alert({
        title: that.data.info.refuse_msg,
      });
    }
    if (refuseAfterToBorrow == 1 || refuseAfterToBorrow == null) {
      that.setData({
        need_tel: 0
      });
    }
  },
  // 用户确认授权
  onGetPhoneAuth() {
    my.getPhoneNumber({
      success: (res) => {
        let encryptedData = res.response;
        console.log(encryptedData);
        util.httpRequest('/getPhoneNumber', {
          encryptedData: encryptedData
        }, function (res) {
          if (res.code == 1) {
            my.showToast({
              content: "授权成功",
              type: 'none',
              duration: 3000
            });
          }
        })
      },
      fail: (res) => {
        console.log(res);
        // console.log('getPhoneNumber_fail');
      },
    });
    // 不管是否授权成功，只要用户点击了授权，则设置成功
    this.setData({
      need_tel: 0
    });
  }

});
